<?php
namespace App\Http\Controllers\Partner;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Models\PartnerModels\Deck;
use App\Components\Tools;
use App\Components\QiniuTools;
class DeckController extends ApiBaseController{
	public function __construct(Request $request) {
		parent::__construct($request);
        $this->imgPath="data/deck/".date('Y-m-d',time())."/";
        $this->ak=Tools::getOneconfig("/config/third.json",array('qiniu','qnAccesskey'));
        $this->sk=Tools::getOneconfig("/config/third.json",array('qiniu','qnSecretkey'));
        $this->bucketName=Tools::getOneconfig("/config/third.json",array('qiniu','qnBucket'));
        $this->url=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
	}
	
	//贴牌管理
	function deckList(){
		$Deck=new Deck();
		$kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
		
		$data=$Deck->Datalist($kid);
		$this->returnJson(0,$data);
	}
	
	//保存贴牌
	function saveDeck(){
		$Deck=new Deck();
		$Qiniu=new QiniuTools();
		$kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
		$data['title']=isset($this->params['title']) ? htmlspecialchars($this->params['title'],ENT_QUOTES) : '';
		
		$checkUrl=\App\Components\Tools::checkUrl($this->params['url']);
		if($this->params['url'] && !$checkUrl){
			$this->returnJson(1,'请正确输入官网链接');
		}
		$data['url']=isset($this->params['url']) ? htmlspecialchars($this->params['url'],ENT_QUOTES) : '';
		
		if($this->params['deck'] && !preg_match("/[0-9a-zA-Z_]{1,}$/",$this->params['deck'])){
			$this->returnJson(1,'请正确输入字母数字下划线的后缀');
		}
		$data['deck']=isset($this->params['deck']) ? htmlspecialchars($this->params['deck'],ENT_QUOTES) : '';
		
		$checkDeck=$Deck->checkDeck($kid,$data['deck']);
		$checkDeck ? $this->returnJson(1,'该后缀已存在') : '';
		
		$deckImg=!empty($_FILES['deckImg']) ? $_FILES['deckImg'] : false;
		$deckIcon=!empty($_FILES['deckIcon']) ? $_FILES['deckIcon'] : false;
		
		if($deckImg){
			$imgType = isset($deckImg['type']) && !empty($deckImg['type']) ? $deckImg['type'] : $this->returnJson(1, 'logo不存在');
			$imgSize = isset($deckImg['size']) && !empty($deckImg['size']) ? $deckImg['size'] : $this->returnJson(1, 'logo不存在');
			if (!in_array($imgType, ['image/pjpeg', 'image/jpeg', 'image/png', 'image/x-png'])) {
				$this->returnJson(1, 'logo格式错误');
			}
			if ($imgSize > 1048576) {
				$this->returnJson(1, '请上传小于1M的logo');
			}
			$data['img']=$Qiniu->upImg($deckImg,"data/deck");
		}
		if($deckIcon){
			$iconType = isset($deckIcon['type']) && !empty($deckIcon['type']) ? $deckIcon['type'] : $this->returnJson(1, 'icon不存在');
			$iconSize = isset($deckIcon['size']) && !empty($deckIcon['size']) ? $deckIcon['size'] : $this->returnJson(1, 'icon不存在');
			if ($iconType!='image/x-icon') {
				$this->returnJson(1, 'icon格式错误');
			}
			if ($iconSize > 1048576) {
				$this->returnJson(1, '请上传小于1M的icon');
			}
			$data['icon']=$Qiniu->upImg($deckIcon,"data/deck");
		}
		
		if($kid && !empty($data['img'])){
			isset($this->params['oldImg']) && !empty($this->params['oldImg']) ? $Qiniu->delFile($this->params['oldImg']) : '';
		}
		if($kid && !empty($data['icon'])){
			isset($this->params['oldIcon']) && !empty($this->params['oldIcon']) ? $Qiniu->delFile($this->params['oldIcon']) : '';
		}
		
		// echo "<pre/>";print_r($kid);
		$data['updateTime']=date('Y-m-d H:i:s');
		$re=$Deck->saveDeck($kid,$data);
		$re ? $this->returnJson(0,'保存成功') : $this->returnJson(1,'保存失败');
	}
	
	
	
}